package com.salim.leetcode.$169;

public class MajorityElement {
    /**
     * 摩尔投票算法 两两抵消
     * https://www.zhihu.com/question/49973163/answer/235921864
     * @param num
     * @return
     */
    public int majorityElement(int[] num) {

        int major=num[0], count = 1;
        for(int i=1; i<num.length;i++){
            if(count==0){
                count++;
                major=num[i];
            }else if(major==num[i]){
                count++;
            }else count--;

        }
        return major;
    }
}
